/**
 * 角色管理的单例
 */
var StandardArea = {
    id: "menuTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
StandardArea.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '编号', field: 'ID', visible: false, align: 'center', valign: 'middle', hidden: true},
        {title: '区域名称', field: 'NAME', align: 'center', valign: 'middle', sortable: true},
        {title: '父级编号', field: 'PARENT_ID', align: 'center', valign: 'middle', sortable: true},
        {title: '区域编号', field: 'CODE', align: 'center', valign: 'middle', sortable: true}]
    return columns;
};


/**
 * 检查是否选中
 */
StandardArea.check = function () {
    var selected = $('#' + this.id).bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        StandardArea.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加区域
 */
StandardArea.openAddMenu = function () {
    var index = layer.open({
        type: 2,
        title: '添加区域',
        area: ['830px', '450px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/standardArea/standardArea_add'
    });
    this.layerIndex = index;
};

/**
 * 点击修改
 */
StandardArea.openChangeMenu = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改区域',
            area: ['800px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/standardArea/standardArea_edit/' + this.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除
 */
StandardArea.delMenu = function () {
    if (this.check()) {

        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/standardArea/remove", function (data) {
                Feng.success("删除成功!");
                StandardArea.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("menuId", StandardArea.seItem.id);
            ajax.start();
        };

        Feng.confirm("是否刪除该区域?", operation);
    }
};

/**
 * 搜索
 */
StandardArea.search = function () {
    var queryData = {};

    queryData['menuName'] = $("#menuName").val();
    queryData['level'] = $("#level").val();

    StandardArea.table.refresh({query: queryData});
}

$(function () {
    var defaultColunms = StandardArea.initColumn();
    var table = new BSTreeTable(StandardArea.id, "/standardArea/list", defaultColunms);
    table.setExpandColumn(2);
    table.setIdField("ID");
    table.setCodeField("ID");
    table.setParentCodeField("PARENT_ID");
    table.setExpandAll(true);
    table.init();
    StandardArea.table = table;
});
